package com.algorithm.base.greedy;

/**
 * 376. 摆动序列
 * 画图以及关注首尾
 *
 * @author Amadeus
 * @date 2023/11/16
 */
public class WiggleMaxLength {

    public int wiggleMaxLength(int[] nums) {
        int length = nums.length;
        if (length < 2) {
            return length;
        }
        int prevdiff = nums[1] - nums[0];
        int num = prevdiff != 0 ? 2 : 1;
        for (int i = 1; i < nums.length; i++) {
            int flag = nums[i] - nums[i - 1];
            if ((flag > 0 && prevdiff <= 0) ||
                    (flag < 0 && prevdiff >= 0)) {
                num++;
                prevdiff = flag;
            }
        }
        return num;
    }

}
